package base.java8.time;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.TimeZone;

/**
 * @author 王霄
 * @date 2021/9/11 22:38
 */
public class InstantDemo {
    public static void main(String[] args) {
        //获取时间戳
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());
        System.out.println("Instant.now().toEpochMilli() = " + Instant.now().toEpochMilli());
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());

        //设置时区
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        TimeZone.setDefault(timeZone);
        System.out.println(LocalDateTime.now());

        //使用偏移量获取时区
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli());

        //使用系统时区获取时间戳
        System.out.println(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

    }
}
